<!DOCTYPE html>


<html lang="zh-CN">


<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    相见恨晚！Python那些不为人知的大坑.md |  
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/dist/main.css">

  
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">

  
<link rel="stylesheet" href="/css/custom.css">

  
  
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

  
  

  

</head>

</html>

<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-develop/相见恨晚！Python那些不为人知的大坑"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  相见恨晚！Python那些不为人知的大坑.md
</h1>
 

    </header>
     
    <div class="article-meta">
      <a href="/2020/11/11/develop/%E7%9B%B8%E8%A7%81%E6%81%A8%E6%99%9A%EF%BC%81Python%E9%82%A3%E4%BA%9B%E4%B8%8D%E4%B8%BA%E4%BA%BA%E7%9F%A5%E7%9A%84%E5%A4%A7%E5%9D%91/" class="article-date">
  <time datetime="2020-11-10T16:00:00.000Z" itemprop="datePublished">2020-11-11</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/develop/">develop</a>
  </div>
  
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">1.9k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">7 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <h1 id="相见恨晚！Python那些不为人知的大坑"><a href="#相见恨晚！Python那些不为人知的大坑" class="headerlink" title="相见恨晚！Python那些不为人知的大坑"></a>相见恨晚！Python那些不为人知的大坑</h1><p>”Python容易“似乎已经成了很多开发者的共识，的确，它的表达方式已经非常接近自然语言。不用像C++那样去考虑指针、内存，也不用像Java那样深入理解JVM。</p>
<p>慢慢的，开发者开始确切的认为”我们写的代码是完全正确的“。</p>
<p>但是，在忽视Python的细节的过程中，会发现，会出现奇奇怪怪的错误。回头定位时，迟迟定位不出问题，在网络上搜索也找不到对应的解决方案，回头看代码，觉得代码<strong>无懈可击</strong>。殊不知，在开发过程中已经犯了很多错误。</p>
<p>本文，就总结5个Python初学者经常会遇到的错误。不瞒各位，其中有些错误我当初也踩过坑，并且迟迟无法逃脱，希望把这些问题总结出来，避免大家再次踩坑！</p>
<h3 id="1-字典拷贝"><a href="#1-字典拷贝" class="headerlink" title="1. 字典拷贝"></a>1. 字典拷贝</h3><p>有些场景下，需要对字典拷贝一个副本。这个副本用于保存原始数据，然后原来的字典去参与其他运算，或者作为参数传递给一些函数。</p>
<p>例如，</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; dict_a &#x3D; &#123;&quot;name&quot;: &quot;John&quot;, &quot;address&quot;:&quot;221B Baker street&quot;&#125;  </span><br><span class="line">&gt;&gt;&gt; dict_b &#x3D; dict_a  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>利用赋值运算法把<code>dict_a</code>赋值给<code>dict_b</code>之后，这2个变量的值是相同的。</p>
<p>你或许会拿着<code>dict_b</code>去参与其他的运算，例如，更新/添加键值对。</p>
<p>但事实却不是你认为的那样，如果你更新或者编辑<code>dict_b</code>，<code>dict_a</code>也会随之改变，详细内容可以了解一下Python可变对象与不可变对象。</p>
<p>下面来看一下效果：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; dict_b[&quot;age&quot;] &#x3D; 26  </span><br><span class="line">&gt;&gt;&gt; dict_b  </span><br><span class="line">&#123;&#39;address&#39;: &#39;221B Baker street&#39;, &#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 26&#125;  </span><br><span class="line">&gt;&gt;&gt; dict_a  </span><br><span class="line">&#123;&#39;address&#39;: &#39;221B Baker street&#39;, &#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 26&#125;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>你会发现，给<code>dict_b</code>添加了一个<code>age:26</code>键值对，<code>dict_a</code>也更新了，这样，我们留一个副本就没有任何意义了。</p>
<p>针对这个问题，可以用Python中的浅拷贝<code>copy</code>、深拷贝<code>deepcopy</code>来解决，下面来看一下，</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; dict_c &#x3D; dict_b.copy()  </span><br><span class="line">&gt;&gt;&gt; dict_c[&quot;location&quot;] &#x3D; &quot;somewhere&quot;  </span><br><span class="line">&gt;&gt;&gt; dict_c  </span><br><span class="line">&#123;&#39;address&#39;: &#39;221B Baker street&#39;, &#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 26, &#39;location&#39;: &#39;somewhere&#39;&#125;  </span><br><span class="line">&gt;&gt;&gt; dict_b  </span><br><span class="line">&#123;&#39;address&#39;: &#39;221B Baker street&#39;, &#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 26&#125;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="2-字典键值"><a href="#2-字典键值" class="headerlink" title="2. 字典键值"></a>2. 字典键值</h3><p>首先，来看一个示例，</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; dict_a &#x3D; dict()  </span><br><span class="line">&gt;&gt;&gt; dict_a  </span><br><span class="line">&#123;&#125;  </span><br><span class="line">&gt;&gt;&gt; dict_a[1] &#x3D; &quot;apple&quot;  </span><br><span class="line">&gt;&gt;&gt; dict_a[True] &#x3D; &quot;mango&quot;  </span><br><span class="line">&gt;&gt;&gt; dict_a[2] &#x3D; &quot;melon&quot;  </span><br><span class="line">&gt;&gt;&gt; dict_a  </span><br><span class="line">&#123;1: &#39;mango&#39;, 2: &#39;melon&#39;&#125;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>你注意到发生了什么吗？</p>
<p>输出字典之后，发现键值<code>True</code>没有了！</p>
<p>这是因为，在Python中，True相当于1、False相当于0，因此，在<code>dict_a[True] = &quot;mango&quot;</code>这行代码里，它把原来键值为<code>1</code>给替换了。</p>
<p>可以来验证一下True相当于1的说法：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; isinstance(True, int)  </span><br><span class="line">True  </span><br><span class="line">&gt;&gt;&gt; True &#x3D;&#x3D; 1  </span><br><span class="line">True  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="3-更新列表或字典"><a href="#3-更新列表或字典" class="headerlink" title="3. 更新列表或字典"></a>3. 更新列表或字典</h3><p>同样，先看一个列表的示例：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; list_a &#x3D; [1,2,3,4,5]  </span><br><span class="line">&gt;&gt;&gt; list_a &#x3D; list_a.append(6)  </span><br><span class="line">&gt;&gt;&gt; list_a  </span><br><span class="line"># 不输出任何内容  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>再看一个字典的示例：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; dict_a &#x3D; &#123;&quot;a&quot; : &quot;b&quot;&#125;  </span><br><span class="line">&gt;&gt;&gt; dict_a &#x3D; dict_a.update(&#123;&quot;c&quot; : &quot;d&quot;&#125;)  </span><br><span class="line">&gt;&gt;&gt; dict_a  </span><br><span class="line"># 不输出任何内容  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>发现，打印<code>list_a</code>和<code>dict_a</code>竟然没有任何内容输出！</p>
<p>这是因为，在Python中列表、字典中的一些方法，如排序、更新、附加、添加等，不会创建不必要的副本，从而提升性能，因此，不需要重新分配给变量。</p>
<p>再看一下正确的方法：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; list_a &#x3D; [1,2,3,4,5]  </span><br><span class="line">&gt;&gt;&gt; list_a.append(6)  </span><br><span class="line">&gt;&gt;&gt; list_a.sort()  </span><br><span class="line">&gt;&gt;&gt; list_a  </span><br><span class="line">[1, 2, 3, 4, 5, 6]  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="4-驻留字符串"><a href="#4-驻留字符串" class="headerlink" title="4. 驻留字符串"></a>4. 驻留字符串</h3><p>在某些情况下，Python尝试重用现有的不可变对象。字符串驻留就是这样一种情况。</p>
<p>来看一个示例对比，</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; a &#x3D; &quot;gmail&quot;  </span><br><span class="line">&gt;&gt;&gt; b &#x3D; &quot;gmail&quot;  </span><br><span class="line">&gt;&gt;&gt; a is b  </span><br><span class="line">True  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>然后修改一下，</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; a &#x3D; &quot;@gmail&quot;  </span><br><span class="line">&gt;&gt;&gt; b &#x3D; &quot;@gmail&quot;  </span><br><span class="line">&gt;&gt;&gt; a is b  </span><br><span class="line">False  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>是不是很神奇？我们只加了一个<code>@</code>符号，结果却截然不同！</p>
<p>在第一个实现方法中，尝试创建两个不同的字符串对象。但是当检查两个对象是否相同时，它返回True。</p>
<p>这是因为python并没有创建另一个对象b，而是将b指向了第一个值<code>gmail</code>，换句话说它被驻留了。</p>
<p>但是，如果字符串中除ASCII字符、数字、下划线以外的其他字符时，它则不会驻留，这样的话，它就不会再指向<code>@gmail</code>。</p>
<p>这里需要注意一下，<code>is</code>与<code>==</code>的运算是不同的。</p>
<p><code>==</code>用于判断<strong>值</strong>是否相等，<code>is</code>不仅需要值相等，还需要指向同一个对象。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; a &#x3D; &quot;@gmail&quot;  </span><br><span class="line">&gt;&gt;&gt; b &#x3D; &quot;@gmail&quot;  </span><br><span class="line">&gt;&gt;&gt; a is b  </span><br><span class="line">False  </span><br><span class="line">&gt;&gt;&gt; a &#x3D;&#x3D; b  </span><br><span class="line">True  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="5-默认参数只计算一次"><a href="#5-默认参数只计算一次" class="headerlink" title="5. 默认参数只计算一次"></a>5. 默认参数只计算一次</h3><p>先看一下下面这个示例：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; def func(a, lst&#x3D;[]):  </span><br><span class="line">...     lst.append(a)  </span><br><span class="line">...     return lst  </span><br><span class="line">...   </span><br><span class="line">&gt;&gt;&gt; print(func(1))  </span><br><span class="line">[1]  </span><br><span class="line">&gt;&gt;&gt; print(func(2))  </span><br><span class="line">[1, 2]  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>这里发生了什么？</p>
<p>我在<code>func</code>中给了一个默认参数<code>[]</code>，然后先后调用2次<code>func</code>函数。</p>
<p>按照我们常规的认识，这2次调用是分开的，第1次调用输出<code>[1]</code>，第二次应该输出<code>[2]</code>，为什么第2次调用时列表里竟然还保留着第1次调用时的值？</p>
<p>这是因为，在Python中，默认参数只会被计算一次。第1次调用<code>func(1)</code>时，它用到了默认参数。但是，第2次调用就不会再去计算默认参数，直接在<code>[1]</code>的基础上附加一个值。</p>
<h3 id="结语"><a href="#结语" class="headerlink" title="结语"></a>结语</h3><p>本文介绍的这些经常犯错的点，或许你还没有遇到过，因此会觉得不以为然。但是，总会在某个时间点、开发某个小模块时不知不觉中用到这5个问题所涵盖的其中某个功能。</p>
<p>如果想当然，按照我们概念里认为的那样去做，结果可想而知，会出现错误。</p>
<p>因此，在Python开发过程中，应该注意一些细节，深入系统的学习一下Python，避免在一些Python基本特性方面犯一些错误。</p>
<p>如果你对Python有深入的了解，自然会避免这些错误。反之，则会给你带来大麻烦，一旦出了错误就很难定位出来。</p>
<hr>
<p><strong>往期精选</strong></p>
<p><a target="_blank" rel="noopener" href="http://mp.weixin.qq.com/s?__biz=MzUzNTMwNzUwNg==&mid=2247483920&idx=1&sn=532aeb760280ec13100092725e45ea94&chksm=fa86378dcdf1be9b182b196386a435c7457bdfe4fae8fa85e70a710a75f075f8e12c945c9c24&scene=21#wechat_redirect">太酷了！一款强大的机器学习可视化IDE</a>  </p>
<p><a target="_blank" rel="noopener" href="http://mp.weixin.qq.com/s?__biz=MzUzNTMwNzUwNg==&mid=2247483929&idx=1&sn=b909dda85c9157878d6c3deb758c5a68&chksm=fa863784cdf1be92d56129f4265a7c0087050ece4ee33b87dcebada9abec37ca826d57fab56f&scene=21#wechat_redirect">太好用！教你几招Python魔法方法的妙用</a>  </p>
<p><a target="_blank" rel="noopener" href="http://mp.weixin.qq.com/s?__biz=MzUzNTMwNzUwNg==&mid=2247483909&idx=1&sn=e2cf46cddf0868384815a229213d82f0&chksm=fa863798cdf1be8e853e24bd22dc0e7128b2126eb88d506ac7a2e6467dfe3f12f36f0318f20c&scene=21#wechat_redirect">GitHub万赞！这个神仙资源一定能够让你的Python技能更上一层楼</a>  </p>
<p><strong>福利</strong></p>
<p>最近我花费了半个月的时间，整理了1份理论+实践的计算机视觉入门教程，这或许是你见过最好的一份CV教程之一。独家打造、完全免费，需要的同学可以扫码添加我的个人微信，发送“<strong>CV</strong>”获取~</p>
<p><img src=""></p>
 
      <!-- reward -->
      
      <div id="reword-out">
        <div id="reward-btn">
          打赏
        </div>
      </div>
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>版权声明： </strong>
          
          本博客所有文章除特别声明外，著作权归作者所有。转载请注明出处！
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://example.com/2020/11/11/develop/%E7%9B%B8%E8%A7%81%E6%81%A8%E6%99%9A%EF%BC%81Python%E9%82%A3%E4%BA%9B%E4%B8%8D%E4%B8%BA%E4%BA%BA%E7%9F%A5%E7%9A%84%E5%A4%A7%E5%9D%91/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/develop/" rel="tag">develop</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2020/11/11/develop/%E7%94%A8Python%E6%93%8D%E4%BD%9CKubernetes%E7%9A%84Job/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            用Python操作Kubernetes的Job.md
          
        </div>
      </a>
    
    
      <a href="/2020/11/11/develop/%E8%BF%99%E6%A0%B7%E7%9A%84%E4%BB%A3%E7%A0%81%E6%89%8D%E5%8F%AB%20Pythonic/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">这样的代码才叫 Pythonic.md</div>
      </a>
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "",
    app_key: "",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "给我的文章加点评论吧~",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2015-2020
        <i class="ri-heart-fill heart_icon"></i> TzWind
      </li>
    </ul>
    <ul>
      <li>
        
        
        
        由 <a href="https://hexo.io" target="_blank">Hexo</a> 强力驱动
        <span class="division">|</span>
        主题 - <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>访问人数:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>浏览次数:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="Hexo"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" target="_blank" rel="noopener" href="http://www.baidu.com">百度</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友链</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/2019/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="搜索">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->


<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->


<link rel="stylesheet" href="/css/clipboard.css">

<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


    
  </div>
</body>

</html>